package com.kaoshi.myweb.webtest.netty;

import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author ZHOUTIEQIANG053
 1. 创建一个ServerBootstrap实例
 2. 创建一个EventLoopGroup来处理各种事件，如处理链接请求，发送接收数据等。
 3. 定义一个远程InetSocketAddress好让客户端连接
 4. 当连接完成之后，Handler会被执行一次   
 5. 所有准备好之后调用ServerBootstrap.connect()方法连接Server
 *
 */
public class NettyClient {

	private final String host;
	private final int prot;

	public NettyClient(String host, int prot) {
		this.host = host;
		this.prot = prot;
	}

	public void start() throws InterruptedException {
		EventLoopGroup group = new NioEventLoopGroup();  
        try {  
            Bootstrap b = new Bootstrap();  
            b.group(group);  
            b.channel(NioSocketChannel.class);  
            b.remoteAddress(new InetSocketAddress(host, prot));  
            b.handler(new ChannelInitializer<SocketChannel>() {  
  
                public void initChannel(SocketChannel ch) throws Exception {  
                    ch.pipeline().addLast(new NettyClientHandler());  
                }  
            });  
            ChannelFuture f = b.connect().sync();  
            f.addListener(new ChannelFutureListener() {  
                  
                public void operationComplete(ChannelFuture future) throws Exception {  
                    if(future.isSuccess()){  
                        System.out.println("client connected");  
                    }else{  
                        System.out.println("server attemp failed");  
                        future.cause().printStackTrace();  
                    }  
                      
                }  
            });  
            f.channel().closeFuture().sync();  
        } finally {  
            group.shutdownGracefully().sync();  
        }  

	}

	public static void main(String[] args) {
		try {
			new NettyClient("127.0.0.1", 9090).start();
		} catch (InterruptedException e) {
			e.printStackTrace();
		};

	}

}
